define(["cmn","biduEditor/third-party/mathquill/mathquill.min","validate","lib/render","lib/diqye-pager"],function(cmn){
	var ejs=cmn.ejs("19911204/2/blogView"),eid=700,_eid=700;
	ejs.set("nav_right","nav_right");
	ejs.set("list","list");
	return {
		init:function(){
			var _=this;
			_.renderMain();
		},
		renderMain:function(){
			var _=this,reviewModel,pageDiv=_.el.find("#review1020");
			_.el.render();
			_.renderActive(_.el.find("#container"));
			reviewModel={
				action:"Blog_queryReview",
				params:{
					currentIndex:0,
					pageSize:10,
					target:BLOG_ID
				}
			};
			var pagefn=function(data){
				if(data.count<=10)return;
				// 分页
				_.el.find("#pagination1020").pagination({
					ejs:cmn.cmn_ejs.pagination,
					param:{
						count:data.count,
						pageSize:10
					},
					callback:function(i){
						reviewModel.params.currentIndex=(i-1)*10;
						_.renderReview(pageDiv,reviewModel);
					}
				});	
			};
			_.renderReview(pageDiv,reviewModel,pagefn);
			_.el.find("#revew1020").on("click",function(){
				if(_.el.find("#form1020").validate()){
					var model={
						action:"Blog_addReview",
						params:{
							target:BLOG_ID,
							content:_.el.find("#revew_countent_1020").val()
						}
					};
					cmn.lbox.show();
					cmn.post(model).then(function(data){
						cmn.lbox.hide();
						reviewModel.params.currentIndex=0;
						_.renderReview(pageDiv,reviewModel,pagefn);
						_.el.find("#form1020").reset();
					});
				}
			});
			_.el.find("#revew_countent_1020").on("keyup",function(e){
				if(e.keyCode==13)_.el.find("#revew1020").trigger('click');
			});
		},
		renderActive:function(div){
			var _=this,
			activites=div.find("button[diqye=active]"),
			acs=div.find("[diqye=active_count]");
			if(activites.size()!=0)active1();
			if(acs.size()!=0)active2();
			
			function active1(){
				activites.each(function(){
					fn1($(this));
				});
				activites.off("click").on("click",function(){
					var btn=$(this);
					cmn.lbox.show();
					cmn.post({
						action:"Blog_joinActiveLi",
						params:{
							activeid:parseInt(btn.attr("activeid")),
							blogid:BLOG_ID
						}
					}).then(function(data){
						cmn.lbox.hide();
						_.renderActive(div);
					});
				});
			}
			function active2(){
				acs.each(function(){
					fn2($(this));
				});
				
			}

			function fn1(btn,data){
				cmn.post({
					action:"Blog_isJoinActive",
					params:{
						activeid:parseInt(btn.attr("activeid"))
					}
				}).then(function(data){
					if(data==1){
						btn.text("你已经参加").addClass("disabled");
					}
					if(data==2){
						btn.text("你未登录").addClass("disabled");
					}
				});
			}
			function fn2(span,data){
				cmn.post({
					action:"Blog_joinCount",
					params:{
						activeid:parseInt(span.attr("activeid"))
					}
				}).then(function(data){
					span.text(data);
				});
			}
			
		},
		renderReview:function(div,reviewModel,callback){
			var _=this;
			cmn.lbox.show();
			cmn.post(reviewModel).then(function(data){
				data.currentIndex=reviewModel.params.currentIndex;
				div.html(ejs.list,data);
				if(callback)callback(data);
				cmn.lbox.hide();
			});
		},
		nav_right:function(div){
			var _=this;
			var navs=[],l,h,item,id=100;
	      		div.find("h1,h2,h3").each(function(){
	      			var _=$(this);
	            	_.attr("nav",true);
	      			if(_.is("h1")){
	      				navs.push({name:_.text(),h:1,point:id});
	      				_.attr("id",id++);
	      			}

	      			if(_.is("h2")){
	      				l=navs.length-1;
	      				item={name:_.text(),h:2,point:id};
	      				_.attr("id",id++);
	      				if(l==-1){
	      					navs.push(item);
	      				}else{
	      					h=navs[l];
	      					if(h.h==1){
	      						if(h.child)h.child.push(item);
	      						else h.child=[item];
	      					}else{
	      						navs.push(item);
	      					}
	      				}
	      			}

	      			if(_.is("h3")){
	      				l=navs.length-1;
	      				item={name:_.text(),h:3,point:id};
	      				_.attr("id",id++);
	      				if(l==-1){
	      					navs.push(item);
	      					return;
	      				}

	      				h=navs[l];
	      				if(h.h==1){
	      					if(h.child){
	      						if(h.child.h==2){
	      							if(h.child.child)h.child.child.push(item);
	      							else h.child.child=[item];
	      							return;
	      						}
	      						h.child.push(item);
	      						return;
	      					}
	      					h.child=[item];
	      					return;
	      				}

	      				if(h.h==2){
	      					if(h.child)h.child.push(item);
	      					else h.child=[item];
	      					return;
	      				}
	      				navs.push(item);
	      			}
	      		});
				
	    		_.el.append(ejs.nav_right,{data:navs},function(){
		            var h = $("[nav]"),a,b;
		    				$(window).on("scroll",function(e){
		              h.each(function(){
		                b=$(this).offset().top;
		                if(document.body.scrollTop>=b){
		                  a=$(this);
		                }
		              });
		              if(a==null)return;
		              $(".bb>li>a").removeClass("active");
		              $(".bb>li>a[href=#"+a.attr("id")+"]").addClass("active");
		            });
	    		});
		}
	}
});